package test;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Datastore_LL_multypool_poc
{

   // DEFAULT is 1
   private static final int NUM_MAIN_ITERATION = 1;
   
   // DEFAULT is 10
   private static final int NUM_THREADS = 20;
   
   // DEFAULT is 50
   private static final int NUM_CALLER_ITERATION = 10;

   /**
    * @param args
    * @throws IOException 
    * @throws InterruptedException 
    */
   public static void main(String[] args) throws IOException, InterruptedException
   {
      System.out.println(">>> alexey: Datastore_LL_multypool_poc.main()");

      for (int j = 0; j < NUM_MAIN_ITERATION; j++)
      {
         System.out.println(">>> alexey: Datastore_LL_multypool_poc.main j = " + j);

         ExecutorService pool = Executors.newFixedThreadPool(NUM_THREADS);

         Set<Future<Long>> results = new HashSet<Future<Long>>(NUM_THREADS);

         // Create pool of executors.
         for (int i = 0; i < NUM_THREADS; i++)
         {
            // To PUT data
            GPlacesPuter worker = new GPlacesPuter(i, NUM_CALLER_ITERATION);
            
            // To DELETE data
            // GPlacesDeleter worker = new GPlacesDeleter(i, 1);
            
            Future<Long> result = pool.submit(worker);
            results.add(result);
         }

         pool.shutdown();

         long sumTime = 0;
         int i = 0;
         for (Future<Long> result : results)
         {
            try
            {
               sumTime += result.get();
               System.out.println(">>> alexey: Datastore_LL_multypool_poc.main result.get() i = " + i + " = "
                  + result.get());
            }
            catch (ExecutionException e)
            {
               e.printStackTrace();
            }
            i++;
         }

         long avgTime = sumTime / NUM_THREADS;
         System.out.println("\n\n\n>>> alexey: Datastore_LL_multypool_poc.main avgTime = " + avgTime);

         System.out.println(">>> alexey: ================================================== \n\n\n\n\n");
      }


   }

}
